<template>
  <div class="app-container">
    <el-card>
      <el-row
        :gutter="20"
        class="query-area"
      >
        <el-col :span="5">
          <span>业务单号</span>
          <el-input
            v-model="queryInfo.no"
            style="width: 65%;"
            placeholder="请输入业务单号"
            size="small"
            clearable
            @clear="clearSearch"
          />
        </el-col>
        <el-col :span="10">
          <span>编制日期</span>
          <el-date-picker
            v-model="rangeDate"
            size="small"
            type="daterange"
            range-separator="至"
            start-placeholder="输入编制日期查询"
            end-placeholder="输入编制日期查询"
            :picker-options="pickerOptions"
            unlink-panels
            @change="changeDateRange"
          />
        </el-col>
        <el-col
          :span="2"
          :offset="1.5"
        >
          <el-button
            type="primary"
            size="small"
            @click="query"
          >查询</el-button>
        </el-col>
      </el-row>
      <div class="main-area">
        <el-row>
          <el-col :span="6">
            <el-button
              type="primary"
              size="small"
              @click="add"
            >新增</el-button>
            <el-button
              type="primary"
              size="small"
              @click="upload"
            >上报</el-button>
            <el-button
              type="primary"
              size="small"
              @click="rescindApply"
            >撤销</el-button>
          </el-col>
          <el-col
            :span="9"
            :offset="9"
          >
            <!-- 分页区域 -->
            <el-pagination
              :current-page="queryInfo.pageNum"
              :page-sizes="[10, 15, 20, 30, 50]"
              :page-size="queryInfo.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="writeOffApply.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-col>
        </el-row>
        <el-table
          v-loading="loading"
          :data="writeOffApply.list"
          :header-cell-style="{'text-align':'center', 'background-color':'#EEF5FD'}"
          :cell-style="{'text-align':'center'}"
          stripe
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" />
          <el-table-column
            label="序号"
            type="index"
            :index="table_index"
            width="60"
          />
          <el-table-column
            label="状态"
            prop="fchangeState"
          />
          <el-table-column
            label="业务单号"
            prop="fno"
          />
          <el-table-column
            label="日期"
            prop="fDate"
          />
          <el-table-column
            label="合计份数"
            prop="fnumber"
          />
          <el-table-column
            label="金额"
            prop="ftotalAmt"
          />
          <el-table-column
            label="备注"
            prop="fmemo"
          />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-tooltip
                content="查看并修改信息"
                placement="top"
                :enterable="false"
              >
                <el-button
                  icon="el-icon-notebook-2"
                  size="small"
                  @click="edit(scope.row)"
                />
              </el-tooltip>
              <el-tooltip
                content="删除"
                placement="top"
                :enterable="false"
              >
                <el-button
                  icon="el-icon-error"
                  size="small"
                  @click="del(scope.row)"
                />
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <bill-dialog
      v-if="dialogVisible"
      :dialog-visible="dialogVisible"
      :is-new="isNew"
      :row="row"
    />

  </div>
</template>

<script>
import { getApplyList, deleteApply, uploadApply, rescindApply } from '@/api/qiuhengGroupApi/writeOff/wirteOffUnit'
import BillVerificationInfo from './billVerificationInfo'
export default {
  components: {
    'bill-dialog': BillVerificationInfo
  },
  data () {
    return {
      // 查询信息
      queryInfo: {
        // 单位识别码，登录时应该保存，在这里读取
        agenIdCode: '1',
        no: '',
        startDate: null,
        endDate: null,
        pageNum: 1,
        pageSize: 10
      },
      rangeDate: [],
      // 限定时间选择不超过今天
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        }
      },
      writeOffApply: {
        // 数据列表
        list: [],
        // 数据总数
        total: 0
      },
      loading: true,
      // 是否显示
      dialogVisible: false,
      // 是否为新增
      isNew: false,
      // 选中的数据
      multipleSelection: [],
      row: {
        fNo: '',
        fChangeState: '',
        fIsUpload: ''
      }
    }
  },
  created () {
    this.getWriteOffApplyList()
  },
  methods: {
    async getWriteOffApplyList () {
      // 获取数据
      const { data: res } = await getApplyList(this.queryInfo)

      this.writeOffApply.list = res.list
      this.writeOffApply.total = res.total
      this.loading = false
    },
    // 查询
    query () {
      this.loading = true
      if (this.rangeDate.length !== 0) {
        this.queryInfo.startDate = this.rangeDate[0].toLocaleDateString()
        // 由于传到后端的数据为当日0时，所以查不到结束日期当天的数据，为此将日期多加一天以达到要求
        // 创建date对象的目的是不修改日期范围选择器中的范围
        const date = new Date()
        date.setTime(this.rangeDate[1].getTime())
        date.setDate(date.getDate() + 1)
        this.queryInfo.endDate = date.toLocaleDateString()
      }
      this.getWriteOffApplyList()
    },
    // 监听 pagesize 改变的事件
    handleSizeChange (newSize) {
      this.loading = true
      this.queryInfo.pageSize = newSize
      this.getWriteOffApplyList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange (newPage) {
      this.loading = true
      this.queryInfo.pageNum = newPage
      this.getWriteOffApplyList()
    },
    // 翻页序号连续
    table_index (index) {
      return (this.queryInfo.pageNum - 1) * this.queryInfo.pageSize + index + 1
    },
    // 清空搜索区
    clearSearch () {
      this.getWriteOffApplyList()
    },
    // 监听日期选择的变化
    changeDateRange () {
      if (this.rangeDate === null) {
        this.queryInfo.startDate = null
        this.queryInfo.endDate = null
        this.clearSearch()
      }
    },
    // 新增申请对话框显示
    add () {
      this.dialogVisible = true
      this.isNew = true
      this.row = {
        fno: '',
        fchangeState: '',
        fisUpload: '',
        fmemo: '',
        fendDate: ''
      }
    },
    // 修改申请对话框显示
    edit (row) {
      this.dialogVisible = true
      this.isNew = false
      this.row = row
    },
    // 关闭对话框
    close () {
      this.dialogVisible = false
    },
    // 删除申请
    del (row) {
      this.$confirm('此操作将永久删除该核销申请, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        if (row.fisUpload !== '已上报' && row.fchangeState !== '已审验') {
          deleteApply(row.fNo)
            .then((res) => {
              if (res.code === 10000) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                this.getWriteOffApplyList()
              } else {
                this.$message({
                  type: 'error',
                  message: '删除失败!'
                })
              }
            })
            .catch((err) => {
              this.$message({
                type: 'error',
                message: '删除失败!'
              })
              console.error(err)
            })
        } else {
          this.$message({
            type: 'info',
            message: '该申请不可删除!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 处理多选框
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 上报申请
    upload () {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'info',
          message: '没有选中的申请'
        })
        return
      }
      try {
        const list = []
        this.multipleSelection.forEach(item => {
          if (item.fisUpload === '已上报') {
            this.$message({
              type: 'warning',
              message: '选中的申请中存在已经上报的申请'
            })
            throw new Error('hasUploadApply')
          }
          list.push(item.fno)
        })
        uploadApply(list)
          .then((res) => {
            if (res.code === 10000) {
              this.$message({
                type: 'success',
                message: '上报成功!'
              })
              this.getWriteOffApplyList()
            } else {
              this.$message({
                type: 'error',
                message: '上报失败!'
              })
            }
          })
          .catch((err) => {
            this.$message({
              type: 'error',
              message: '上报失败!'
            })
            console.error(err)
          })
      } catch (e) {
        if (e.message !== 'hasUploadApply') console.error(e)
      }
    },
    // 撤销申请
    rescindApply () {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'info',
          message: '没有选中的申请'
        })
        return
      }
      try {
        const list = []
        this.multipleSelection.forEach(item => {
          if (item.fchangeState === '已审验') {
            this.$message({
              type: 'warning',
              message: '选中的申请中存在已经被审验'
            })
            throw new Error('err')
          }
          if (item.fisUpload === '未上报') {
            this.$message({
              type: 'warning',
              message: '选中的申请中存在未上报的'
            })
            throw new Error('err')
          }
          list.push(item.fno)
        })
        rescindApply(list)
          .then((res) => {
            if (res.code === 10000) {
              this.$message({
                type: 'success',
                message: '撤销成功!'
              })
              this.getWriteOffApplyList()
            } else {
              this.$message({
                type: 'error',
                message: '无法撤销!'
              })
            }
          })
          .catch((err) => {
            this.$message({
              type: 'error',
              message: '撤销失败!'
            })
            console.error(err)
          })
      } catch (e) {
        if (e.message !== 'err') console.error(e)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .el-card {
    .query-area {
      margin-bottom: 5px;
    }
    .main-area {
      margin-top: 20px;
    }
    .el-col {
      text-align: center;
      span,
      .el-input,
      .el-date-editor {
        margin-left: 5px;
      }
      .el-button {
        margin-left: 30px;
      }
    }
    .el-table {
      margin-top: 10px;
      .el-button {
        border: none;
        background-color: transparent;
        padding-top: 0;
        padding-bottom: 0;
        font-size: 18.5px;
      }
    }
  }
}
</style>

<style lang="scss">
body .el-table th.gutter {
  display: table-cell !important;
}
</style>

